package com.newhope.srm.purchase;

import com.newhope.srm.common.shared.constant.ModelConstants;
import org.apache.ibatis.annotations.Select;
import com.newhope.srm.purchase.shared.enums.ContractTypeEnum;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.sql.DataSource;
import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@EnableScheduling
@EnableFeignClients
@MapperScan({"com.newhope.srm.purchase.infrastructure.db.mapper"})
@ComponentScan({ModelConstants.COMMON_MODULE_PACKAGE, ModelConstants.PURCHASE_MODULE_PACKAGE, "com.newhope.srm.purchase.shared.message"})
public class PurchaseApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(PurchaseApplication.class, args);
        System.out.println("===============采购模块启动完成==============");
        String port = context.getEnvironment().getProperty("server.port");
        String path = context.getEnvironment().getProperty("server.servlet.context-path");
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(String.format("=============== http://%s:%s%s/doc.html", hostAddress, port, path));
    }

    /**
     * 启动就创建连接
     *
     * @param dataSource
     * @return
     */
    @Bean
    public ApplicationRunner runner(DataSource dataSource) {
        return args -> dataSource.getConnection();
    }
}
